package com.leetcode.problems028;


/**
 * https://leetcode-cn.com/problems/implement-strstr/
 * @Date 2021/11/20 14:34
 * @Author ghery
 * @Version V1.0
 */
public class Solution {

    /**
     * 超出时间限制
     * @param haystack
     * @param needle
     * @return
     */
    public int strStr(String haystack, String needle) {
        int hl = haystack.length();
        int nl = needle.length();
        if(nl == 0){
            return 0;
        }
        if(hl < nl){
            return -1;
        }
        for (int i = 0; i < hl && i + nl <= hl;i++) {
            int j = 0;
            int ti = i;
            while(j < nl && ti < hl && haystack.charAt(ti) == needle.charAt(j)) {
               j++;
               ti++;
            }
            if(j == nl){
                return ti - nl;
            }
        }
        return -1;
    }


    public static void main(String[] args) {
        String str = "hello";
        String str1 = "ll";
        System.out.println(new Solution().strStr(str,str1));
    }
}
